<?php

	require_once APPPATH . 'controllers/base_controllers/base_controller.php';

	class Walk_registration extends Base_controller
	{
		public static $REGISTRATION_TEMPLATE = 'templates/registration/registration_template';		
		
		
		public function __construct()
		{
			parent::__construct();
			
			$this->load->model('values/walk_registration_value');
			$this->load->model('values/person_value');
			$this->load->model('values/walk_info_value');
		}
		
		
		public function step1()
		{
			//Create Registration Value In Session
			$walkRegistrationValue = new Walk_registration_value();
			
			$this->session->unset_userdata('walk_registration_value');
			$this->session->set_userdata('walk_registration_value', serialize($walkRegistrationValue));
			
			$data['title'] = 'Walk/Run for CORD 2011 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;New Registrant Information';
			$data['view'] = 'walk/register/walk_collect_person_view';
			
			$this->load->view( self::$REGISTRATION_TEMPLATE, $data );
		}
		
		
		public function step1_process()
		{
			$this->load->model('proxies/Person_proxy');
			$this->load->model('proxies/Walk_proxy');
			$this->load->library('encrypt');
			
			$curyear = $this->Walk_proxy->getCurrentYear();
			
			//Save To Session...
			$person = new Person_value();
			$person->setPersonName( $this->input->post('userName') );
			$person->setPassword( $this->encrypt->encode($this->input->post('password')) );
			$person->setFirstName( $this->input->post('firstName') );
			$person->setLastName( $this->input->post('lastName') );
			$person->setGender( $this->input->post('gender') );
			$person->setEmail1( $this->input->post('primaryEmail') );
			$person->setPhone1( $this->input->post('homePhoneNumber') );
			$person->setPhone2( $this->input->post('mobilePhoneNumber') );
			$person->setAddress1( $this->input->post('homeAddress1') );
			$person->setAddress2( $this->input->post('homeAddress2') );
			$person->setCity( $this->input->post('city') );
			$person->setState( $this->input->post('state') );
			$person->setZip( $this->input->post('zipCode') );
			$person->setNumberOfChildren( $this->input->post('numberOfChildrenAttendingBalvihar') );
			
			$continueWithRegistration = FALSE;
			$insertPerson = FALSE;
			$updatePerson = FALSE;
			$existingPersonId = 0;
			
			$checkPersonExists = $this->Person_proxy->personExists($person);
			if( $checkPersonExists == 0)
			{
				$continueWithRegistration = TRUE;
				$insertPerson = TRUE;
				$updatePerson = FALSE;
				$existingPersonId = 0;
			}
			else
			{
				$existingPersonId = $checkPersonExists;
				
				if( $this->Person_proxy->personNameIsNull($existingPersonId) )
				{
					$householdXrefId = $this->Person_proxy->personIdInHouseholdXref( $existingPersonId );
					if( $householdXrefId != 0)
					{
						if( $this->Person_proxy->isHouseholdRegistered( $householdXrefId, $curyear ) )
						{
							$continueWithRegistration = FALSE;
						}
						else
						{
							if( $this->Walk_proxy->isPersonRegistered($existingPersonId, $curyear))
							{
								$continueWithRegistration = FALSE;
							}
							else
							{
								$continueWithRegistration = TRUE;
								$insertPerson = FALSE;
								$updatePerson = TRUE;
								$existingPersonId = $existingPersonId;
							}
						}
					}
					else
					{
						if( $this->Walk_proxy->isPersonRegistered($existingPersonId, $curyear))
						{
							$continueWithRegistration = FALSE;
						}
						else
						{
							$continueWithRegistration = TRUE;
							$insertPerson = FALSE;
							$updatePerson = TRUE;
							$existingPersonId = $existingPersonId;
						}
					}
				}
				else
				{
					if( $this->Walk_proxy->isPersonRegistered($existingPersonId, $curyear))
					{
						$continueWithRegistration = FALSE;
					}
					else
					{
						$continueWithRegistration = TRUE;
						$insertPerson = FALSE;
						$updatePerson = FALSE;
						$existingPersonId = $existingPersonId;	
					}
				}
				
			}
			
			if( $continueWithRegistration )
			{
				$walkRegistrationValue = unserialize($this->session->userdata('walk_registration_value'));
				
				$walkRegistrationValue = Walk_registration_value::cast($walkRegistrationValue);
				$walkRegistrationValue->setPerson( $person );
				$walkRegistrationValue->setInsertPerson( $insertPerson );
				$walkRegistrationValue->setUpdatePerson( $updatePerson );
				$walkRegistrationValue->setExistingPersonId( $existingPersonId );
				
				$this->session->unset_userdata('walk_registration_value');
				$this->session->set_userdata('walk_registration_value', serialize($walkRegistrationValue));
				
				redirect( base_url() . 'index.php/walk/walk_registration/step2/');
			}
			else
			{
				redirect( base_url() . 'index.php/walk/walk_registration/member_registered/');
			}
			
			die;
		}
		
		
		public function step2()
		{
			$data['title'] = 'Walk/Run for CORD 2011 Registration';
			$data['view'] = 'walk/register/walk_collect_cwr_view';
			
			$this->load->view( self::$REGISTRATION_TEMPLATE, $data );
		}
		
		
		public function step2_process()
		{
			//Save To Session...
			if( ($this->input->post('numWalkers') + $this->input->post('numRunners')) > 1 )
			{
				$individualOrFamily = 'family';
			}
			else
			{
				$individualOrFamily = 'individual';
			}
			
			if( $this->input->post('goal') == 0 || trim($this->input->post('goal')) == "" )
			{
				$goal = 20;
				if( $individualOrFamily == 'family' )
				{
					$goal = 40;
				}
			}
			else
			{
				$goal = $this->input->post('goal');
			}
			
			
			$walkInfo = new Walk_info_value();
			$walkInfo->setIndividualOrFamily( $individualOrFamily );
			$walkInfo->setDisplayName( $this->input->post('displayName') );
			$walkInfo->setNumWalkers( $this->input->post('numWalkers') );
			$walkInfo->setNumRunners( $this->input->post('numRunners') );
			$walkInfo->setAdditionalTShirts( $this->input->post('additionalTShirts') );
			$walkInfo->setIncludedTShirtSize( $this->input->post('includedTShirtSize') );
			$walkInfo->setGoal( $goal );
			$walkInfo->setDonation( $this->input->post('donation') );
			$walkInfo->setHearAbout( $this->input->post('hearAbout') );
			
			$additionalTShirtSizeArray = array();
			if( $walkInfo->getAdditionalTShirts() > 0 )
			{
				for( $i = 1; $i <= $walkInfo->getAdditionalTShirts(); $i++ )
				{
					array_push( $additionalTShirtSizeArray, $this->input->post('additionalTShirtSize' . $i) );
				}
			}
			$walkInfo->setAdditionalTShirtSizes( $additionalTShirtSizeArray );
			
				
			$walkRegistrationValue = unserialize($this->session->userdata('walk_registration_value'));
			
			$walkRegistrationValue = Walk_registration_value::cast($walkRegistrationValue);
			$walkRegistrationValue->setWalkInfo( $walkInfo );
			
			$this->session->unset_userdata('walk_registration_value');
			$this->session->set_userdata('walk_registration_value', serialize($walkRegistrationValue));
			
			redirect( base_url() . 'index.php/walk/walk_registration/step3/');
			die;
		}
		
		
		public function step3()
		{
			$this->load->model('proxies/Paypal_proxy');
			
			$walkRegistrationValue = $this->session->userdata('walk_registration_value');
			$paypalTempId = $this->Paypal_proxy->addTempData( $walkRegistrationValue );
			
			$walkRegistrationValue = unserialize($this->session->userdata('walk_registration_value'));
			$walkRegistrationValue = Walk_registration_value::cast($walkRegistrationValue);
			
			$data['title'] = 'Registration Summary';
			$data['view'] = 'walk/register/walk_registration_summary_view';
			$data['walkRegistrationValue'] = $walkRegistrationValue;
			$data['paypal_form'] = $this->getPayPalForm( $walkRegistrationValue->getTotalFee(), $paypalTempId );
			
			$this->load->view( self::$REGISTRATION_TEMPLATE, $data );
		}
		
		
		private function getPayPalForm( $amount, $paypalTempId )
		{
			$this->load->library('Paypal_Lib');
			
		    $this->paypal_lib->add_field('return', site_url('/walk/walk_registration/success/'));
		    $this->paypal_lib->add_field('cancel_return', site_url('/walk/walk_registration/cancel/'));
		    $this->paypal_lib->add_field('notify_url', site_url('/walk/walk_registration/ipn/')); // <-- IPN url
		    $this->paypal_lib->add_field('custom', $paypalTempId); // <-- Verify return
	
		    $this->paypal_lib->add_field('item_name', 'CORD Walk 2011 Registration');
		    $this->paypal_lib->add_field('item_number', '');
		    $this->paypal_lib->add_field('amount', $amount);
	
		    return $this->paypal_lib->paypal_form();			
		}
		
		
		public function member_registered()
		{
			$data['title'] = 'You or Someone in your Household is Already Registered';
			$data['view'] = 'walk/register/walk_registration_members_exist_view';
			
			$this->load->view( self::$REGISTRATION_TEMPLATE, $data );
		}
		
		
		public function success()
		{
			$data['title'] = 'Registration Success';
			$data['view'] = 'walk/register/walk_registration_success_view';
			
			$this->load->view( self::$REGISTRATION_TEMPLATE, $data );
		}
		
		
		public function cancel()
		{
			redirect( base_url() );
			die;
		}
			
		public function ipn()
		{
			$this->load->model('proxies/Paypal_proxy');
			$this->load->library('Paypal_Lib');
			
			if( $this->paypal_lib->validate_ipn() ) 
			{
				$this->load->model('proxies/Person_proxy');
				$this->load->model('proxies/Walk_proxy');
				
				$walkRegistrationValue = unserialize($this->Paypal_proxy->getTempData( $this->paypal_lib->ipn_data['custom'] ));
				$walkRegistrationValue = Walk_registration_value::cast( $walkRegistrationValue );
				
				$person = $walkRegistrationValue->getPerson();
				
				if( $walkRegistrationValue->getInsertPerson() )
				{
					$personId = $this->Person_proxy->insertPerson( $person );
				}
				else if( $walkRegistrationValue->getUpdatePerson() )
				{
					$person->setPersonId( $walkRegistrationValue->getExistingPersonId());
					
					$personId = $this->Person_proxy->updatePerson( $person );
				}
				else
				{
					$personId = $walkRegistrationValue->getExistingPersonId();
				}
				
				$cwr_reg_history_id = $this->Walk_proxy->insertWalkRegistration( $personId, $walkRegistrationValue, $this->paypal_lib->ipn_data['txn_id'] );
				
				$this->Paypal_proxy->removeTempData( $this->paypal_lib->ipn_data['custom'] );
			
				$this->Walk_proxy->sendWalkRegistrationEmail( $walkRegistrationValue, $cwr_reg_history_id );
			}
		}
		
		
	}

?>